package com.xxx.web.servlet;

import com.xxx.pojo.Employee;
import com.xxx.service.Services;
import com.xxx.service.impl.EmployeeServiceImpl;
import com.xxx.util.GetIdTime;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/employee/*")
public class EmployeeServlet extends BaseServlet {

    private Services service = new EmployeeServiceImpl();

    private Long id = GetIdTime.getId();
    public void selectByUsername(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        Employee employee =(Employee)service.selectByUsername(username);

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (password.equals(employee.getPassword())){
            System.out.println("登入成功");
            resp.sendRedirect("/LS01_Jsp/login/index.html");
        }else {
            System.out.println("账号密码错误");
            writer.write("<h3>账号密码错误</h3>");
            writer.write("<h3>2秒后返回主界面</h3>");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            resp.sendRedirect("/LS01_Jsp/index.html");
        }

    }

    public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Employee> employees = service.selectAll();
        System.out.printf("selectAll:employees");
        req.setAttribute("employees",employees);
        req.getRequestDispatcher("/login/employee.jsp").forward(req,resp);

    }

    public void addEmployee(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        Long id = Long.valueOf(GetIdTime.getId() + "2");
        String name = req.getParameter("name");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        String sex = req.getParameter("sex");
        String idNumber = req.getParameter("idNumber");
        Integer status = Integer.valueOf(req.getParameter("status"));
        String createTime = GetIdTime.getDataTime();
        Long createUser = Long.valueOf("12345678");


        Employee employee = new Employee(id,name,username,password,phone,sex,idNumber,status,createTime,createTime,createUser,createUser);


        int i = service.add(employee);

        selectAll(req,resp);
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        Long id = Long.valueOf(req.getParameter("id"));
        int delete = service.delete(id);
        selectAll(req,resp);
    }

    public void selectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        Long id = Long.valueOf(req.getParameter("id"));

        Employee employee = (Employee)service.selectById(id);

        req.setAttribute("employee", employee);

        selectAll(req, resp);
    }


    public void updateById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        req.setCharacterEncoding("UTF-8");

        Long id = Long.valueOf(req.getParameter("id"));
        String name = req.getParameter("name");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        String sex = req.getParameter("sex");
        String idNumber = req.getParameter("idNumber");
        Integer status = Integer.valueOf(req.getParameter("status"));

        Employee employee = new Employee();

        employee.setId(id);
        employee.setName(name);
        employee.setPassword(password);
        employee.setUsername(username);
        employee.setPhone(phone);
        employee.setSex(sex);
        employee.setIdNumber(idNumber);
        employee.setStatus(status);
        employee.setUpdateTime(GetIdTime.getDataTime());
        employee.setUpdateUser(this.id);


        int i = service.updateById(employee);
        selectAll(req,resp);
    }


}
